<?php
/**
 * @Author: Xu Liu email:2867557054@qq.com
 * @Date:   2016-12-15 13:31:33
 * @Last Modified by:   Xu Liu
 * @Last Modified time: 2018-02-24 12:44:40
 */
if (!defined('IN_DISCUZ')) {
    exit('Access Denid');
}
class table_jamesonread_buytids extends discuz_table
{
    public function __construct()
    {
        $this->_table = 'jamesonread_buytids';
        $this->_pk    = 'id';
        $this->_pre_cache_key = 'jamesonreadbuytid_';
        $this->_cache_ttl     = 300;
        parent::__construct();
    }
    // 获取用户购买的某书的所有tid
    public function fetch_by_book_uid($uid,$book_id)
    {
        $data = DB::fetch_all('SELECT tid FROM %t WHERE buy_id=%d AND book_id=%d',array($this->_table,$uid,$book_id),'tid');
        if($data){
            return array_keys($data);
        }
        return array();
    }
    // 根据uid和tid判断用户是否购买过章节
    public function hasBuyTid($buy_id, $tid)
    {
        return (int) DB::result_first('SELECT count(buy_id) FROM %t WHERE buy_id=%d AND tid=%d', array($this->_table, $buy_id, $tid));
    }
    // 销售处理
    public function sellaction($buy_id, $author_id, $price, $tid, $book_id, $category_id, $trameid, $yongjinbi)
    {
        // 计算佣金
        $yongjin = floor($price * ($yongjinbi / 100));
        // 作者收入=$price-$yongjin
        $shouru = $price - $yongjin;
        // 购买者减去 bookmoney
        $res1 = updatemembercount($buy_id, array($trameid => -$price), true);
        $res2 = updatemembercount($author_id, array($trameid => $shouru), true);
        // admin增加 yongji
        if($yongjin>0){
            $res3 = updatemembercount(1, array($trameid => $yongjin), true);
        }
        // 佣金表插入记录
        DB::insert('jamesonread_yongjin', array(
            'buy_id'    => intval($buy_id),
            'author_id' => intval($author_id),
            'book_id'   => intval($book_id),
            'saleprice' => intval($price),
            'yongjinbi' => intval($yongjinbi),
            'yongjin'   => $yongjin,
            'type'      => 1,
            'addtime'   => TIMESTAMP,
        ));
        $this->insert(array(
            'buy_id'      => intval($buy_id),
            'author_id'   => intval($author_id),
            'tid'         => intval($tid),
            'category_id' => intval($category_id),
            'addtime'     => TIMESTAMP,
            'price'       => intval($shouru),
            'book_id'     => intval($book_id),
        ), true);
        return !($res1 || $res2);
    }
    // 当前的销售收入
    public function fetch_sale_byuid($uid)
    {
        return (int) DB::result_first('SELECT sum(price) FROM %t WHERE author_id=%d', array($this->_table, $uid));
    }
    public function count_sale_bybookid($book_id)
    {
        return (int) DB::result_first('SELECT sum(price) FROM %t WHERE book_id=%d', array($this->_table, $book_id));
    }
    public function fetch_tids_by_uid($uid)
    {
        return DB::fetch_all('SELECT c.tid,c.pid,c.category_id,c.book_id,c.subject,c.uid,buy.price,buy.addtime,buy.buy_id FROM %t AS c INNER JOIN %t AS buy ON c.tid=buy.tid WHERE buy.buy_id=%d', array('jamesonread_colums', $this->_table, $uid));
    }
    public function count_by_uid($uid)
    {
        if($cache = $this->fetch_cache('countbyuid_'.$uid)){
            return $cache;
        }
        $data = (int) DB::result_first('SELECT count(tid) FROM %t WHERE buy_id=%d', array($this->_table, $uid));
        $this->store_cache('countbyuid_'.$uid,$data);
        return $data;
    }
    public function fetch_by_page($uid,$start,$size)
    {
        $cacheid = 'fetch_by_page-'.$uid.'-'.$start.'-'.$size;
        if($cache = $this->fetch_cache($cacheid)){
            return $cacheid;
        }
        $data = DB::fetch_all('SELECT c.tid,c.pid,c.category_id,c.book_id,c.subject,c.uid,y.addtime,y.price,y.buy_id FROM %t AS c INNER JOIN %t AS y ON c.tid=y.tid WHERE y.buy_id=%d ORDER BY y.addtime DESC LIMIT %d,%d', array('jamesonread_colums', $this->_table, $uid, $start, $size));
        $this->store_cache($cacheid,$data);
        return $data;
    }
    function count_by_bookid($book_id){
        return (int) DB::result_first('SELECT count(tid) FROM %t WHERE book_id=%d',array($this->_table,$book_id));
    }
    function fetch_by_bookid($book_id,$start,$size){
        $cacheid = 'fetch_by_bookid-'.$book_id.'-'.$start.'-'.$size;
        if($cache = $this->fetch_cache($cacheid)){
            return $cacheid;
        }
        $data = DB::fetch_all('SELECT c.tid,c.pid,c.subject,c.uid,c.category_id,c.book_id,y.price,y.addtime,y.buy_id,b.book_name,m.username FROM %t AS c INNER JOIN %t AS y ON c.tid=y.tid INNER JOIN %t AS b ON y.book_id=b.book_id INNER JOIN %t AS m ON y.buy_id=m.uid WHERE y.book_id=%d ORDER BY y.addtime DESC LIMIT %d,%d', array('jamesonread_colums', $this->_table, 'jamesonread_books','common_member',$book_id, $start, $size));
        $this->store_cache($cacheid,$data);
        return $data;
    }
     public function count_by_authorid($uid)
    {
        if($cache = $this->fetch_cache('countbyauthorid_'.$uid)){
            return $cache;
        }
        $data = (int) DB::result_first('SELECT count(tid) FROM %t WHERE author_id=%d', array($this->_table, $uid));
        $this->store_cache('countbyauthorid_'.$uid,$data);
        return $data;
    }
    public function fetch_by_authorid_page($uid,$start,$size)
    {
        $cacheid = 'fetch_by_authorid_page-'.$uid.'-'.$start.'-'.$size;
        if($cache = $this->fetch_cache($cacheid)){
            return $cacheid;
        }
        $data = DB::fetch_all('SELECT c.tid,c.pid,c.category_id,c.subject,c.uid,y.price,y.addtime,y.buy_id,b.book_name,m.username FROM %t AS c INNER JOIN %t AS y ON c.tid=y.tid INNER JOIN %t AS b ON y.book_id=b.book_id INNER JOIN %t AS m ON  y.buy_id=m.uid WHERE y.author_id=%d ORDER BY y.addtime DESC LIMIT %d,%d', array('jamesonread_colums', $this->_table, 'jamesonread_books','common_member',$uid, $start, $size));
        $this->store_cache($cacheid,$data);
        return $data;
    }
}